package com.ray.depot.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.depot.service.StockInOutService;
import com.ray.depot.table.customMapper.SelectStockInOutMapper;
import com.ray.depot.table.dto.StockInOutDto;
import com.ray.depot.table.entity.StockInOut;
import com.ray.depot.table.entity.StockInOutExample;
import com.ray.depot.table.entity.StockInOutExample.Criteria;
import com.ray.depot.table.mapper.StockInOutMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class StockInOutServiceImpl extends BaseService implements StockInOutService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(StockInOutServiceImpl.class);
	
	@Autowired
	private StockInOutMapper stockInOutMapper;
	
	@Autowired
	private SelectStockInOutMapper selectStockInOutMapper;

	@Transactional
	public Result<Object> addStockInOut(StockInOut params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=stockInOutMapper.insertSelective(params);
		if(count!=1){
			logger.error("StockInOutServiceImpl=>addStockInOut is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public StockInOut queryStockInOut(String id, LoginUser user) {
		return stockInOutMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	public Result<Object> updateStockInOut(StockInOut params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=stockInOutMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockInOutServiceImpl=>editStockInOut is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteStockInOut(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		StockInOut params=new StockInOut();
		params.setId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=stockInOutMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockInOutServiceImpl=>deleteStockInOut is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<StockInOut> queryStockInOut(StockInOut params, PageDto<StockInOut> page, LoginUser user) {
		StockInOutExample example=new StockInOutExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(stockInOutMapper.countByExample(example));
		page.setRecords(stockInOutMapper.selectByExample(example));
		return page;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<StockInOutDto> querySelectStockInOut(StockInOutDto params, PageDto<StockInOutDto> page, LoginUser user) {
	    params.setTenantId(user.getTenantId());
	    params.setOrderByClause(page.getOrderBy());
	    params.setLimitStart((page.getPage() - 1)  * page.getPageSize());
	    params.setLimitEnd(page.getPageSize());
	    params.setIsDelete(DeleteEnum.USE.getValue());
		page.setTotal(selectStockInOutMapper.countByExample(params));
		page.setRecords(selectStockInOutMapper.selectByExample(params));
		return page;
	}

}
